package cn.rongdao.jrkabao.ui.message;


import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.FRBaseTakePhotoActivity;
import cn.rongdao.jrkabao.bean.message.ValidateIDCardPhotoBean;
import cn.rongdao.jrkabao.model.information.AddUserIdCardOtherPhotoModel;
import cn.rongdao.jrkabao.model.message.IdCardPhotoModel;
import cn.rongdao.jrkabao.model.message.MessageRebackModel;

/**
 * Created by mifeng on 2016/7/1.
 * 消息回退界面
 */
public class MessageRebackActivity extends FRBaseTakePhotoActivity {

    @BindView(R.id.activity_indentification_card_upload_front_iv)
    RoundedImageView mFrontImageButton;
    @BindView(R.id.activity_indentification_card_upload_opposite_iv)
    RoundedImageView mOppositeImageNutton;
    @BindView(R.id.activity_indentification_card_upload_hand_in_iv)
    RoundedImageView mHandInPicImageButton;

    @BindView(R.id.avic_titlebar)
    TitleBarUIComponent mTitleBarUIComponent;

    private TitleBarUIComponent titleBarUIComponent;
    private String human = "";
    private ImageView imageView = null;

    private Map<String, String> map = new ArrayMap<>();
    private String mType;

    protected void initData() {
        //判断是否是从消息详情进来的
        mType = getIntent().getStringExtra("type");

        getUserPicData();
    }

    private void getUserPicData() {

        IdCardPhotoModel.getInstance().execute(map, new FRBaseObserver<ValidateIDCardPhotoBean>(this) {
            @Override
            protected void onBaseNext(ValidateIDCardPhotoBean data) {
                mAlbumStatus = data.getIs_open_photos();
                FRImageLoad.loadImage(data.getHuman(),mHandInPicImageButton,R.drawable.indentification_card_upload_hand_in);
                FRImageLoad.loadImage(data.getObverse(),mFrontImageButton,R.drawable.indentification_card_upload_front);
                FRImageLoad.loadImage(data.getReverse(),mOppositeImageNutton,R.drawable.indentification_card_upload_opposite);
            }
        });
    }


    @Override
    protected void initView() {
        super.initView();
        initData();
        titleBarUIComponent = findViewById(R.id.avic_titlebar);
        initTitleBar();

        mFrontImageButton = findViewById(R.id.activity_indentification_card_upload_front_iv);
        mOppositeImageNutton = findViewById(R.id.activity_indentification_card_upload_opposite_iv);
        mHandInPicImageButton = findViewById(R.id.activity_indentification_card_upload_hand_in_iv);

    }

    private void initTitleBar() {
        titleBarUIComponent.initTitleBlack("身份证照片");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.initRightBlack("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upLoadPicData();
            }
        });
    }


    @OnClick({R.id.activity_indentification_card_upload_front_iv, R.id.activity_indentification_card_upload_opposite_iv, R.id.activity_indentification_card_upload_hand_in_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_indentification_card_upload_front_iv:
                break;
            case R.id.activity_indentification_card_upload_opposite_iv:
                break;
            case R.id.activity_indentification_card_upload_hand_in_iv:
                if (!lackPermission()) {
                    picType = PIC_WITH_PERSON;
                    caremaOptionsDialog.show();
                    imageView = mHandInPicImageButton;
                }
                break;

        }
    }


    private void upLoadPicData() {
        if (human == null || human.equals("")) {
            Toast.makeText(this, "手持身份证照片还没有修改哦.", Toast.LENGTH_LONG).show();
            return;
        }

        map.put("human", FRString.valueOf(human));
        AddUserIdCardOtherPhotoModel.getInstance().execute(map, new FRBaseObserver<String>(this, "正在上传，请稍候...") {
            @Override
            protected void onBaseNext(String data) {
                saveModifyStatus();
                FRToast.showToastSafe("上传成功");
            }
        });


    }

    private void saveModifyStatus() {
        MessageRebackModel.getInstance().execute(map, new FRBaseObserver<String>(this) {
            @Override
            protected void onBaseNext(String data) {
                if ("modify".equals(mType)) {
                    saveModifyStatus();
                    Intent intent = new Intent();
                    intent.putExtra("data", "modify_success");
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });
    }

    @Override
    protected void successReturnPicpath(String picPath, String suffix,String imageviewBase64) {
        map.put("suffix", FRString.valueOf(suffix));
        Glide.with(this).load(picPath).into(imageView);//利用Glide加载图片，防止内存溢出
        human = imageviewBase64;
    }


    @Override
    protected int layoutRes() {
        return R.layout.activity_message_reback;
    }
}

